package office.stock.messaging;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@Singleton
public class OffersSubscribersRegistry implements SubscribersRegistry {
    
    private final static Log log = LogFactory.getLog(OffersSubscribersRegistry.class);
    
    private Map<String, WebsocketSubscriber> subscribers = new ConcurrentHashMap<>();
    
    @PostConstruct
    public void initialization() {
	log.debug("created");
    }
    
    @Override
    public void addSubscriber(WebsocketSubscriber subscriber) {
	log.debug("Adding new subscriber with session id " + subscriber.getId());
	subscribers.put(subscriber.getId(), subscriber);
    }
    
    @Override
    public void removeSubscriber(String id) {
	log.debug("Removing subscriber with session id " + id);
	subscribers.remove(id);
    }
    
    @Override
    public Collection<WebsocketSubscriber> getAll() {
	return subscribers.values();
    }
}
